package com.wdb007.venus.wss.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wdb007.venus.svc.facade.ProcessInstructionService;
import com.wdb007.venus.svc.facade.process.model.ProcessInstructionLogDTO;
import com.wdb007.venus.wss.channelgroup.BookCaseChannelGroups;
import com.wdb007.venus.wss.constants.PushActionEnum;
import com.wdb007.venus.wss.service.WebSocketServerService;

import io.netty.channel.ChannelHandlerContext;

@Service("bookCaseReqService")
public class BookCaseReqService implements WebSocketServerService {

private static Logger logger = LoggerFactory.getLogger(BookCaseReqService.class);

	@Autowired
	private ProcessInstructionService processInstructionService; 
	
	@Override
	public String doService(ChannelHandlerContext ctx, String request) throws Exception {
		JSONObject jsonObj = JSON.parseObject(request);
		logger.info("BookCaseReq 接收websocket响应信息: {}", jsonObj.toJSONString());
		
		String bookCaseId = jsonObj.getString("bookCaseId");
		String pushId = "";
		ProcessInstructionLogDTO pi = new ProcessInstructionLogDTO();
		pi.setContent(jsonObj.toJSONString());
		pi.setBookgridCode(bookCaseId);
		if(PushActionEnum.bookcase_check_req.name().equals(jsonObj.get("action"))) {
			pushId = processInstructionService.bookCaseCheckReq(pi) +"";
		}else if(PushActionEnum.bookcase_unlock_req.name().equals(jsonObj.get("action"))) {
			pushId = processInstructionService.bookCaseUnlockReq(pi) + "";
		}else if(PushActionEnum.bookcase_status_req.name().equals(jsonObj.get("action"))) {
			pushId = processInstructionService.bookCaseStatusReq(pi) + "";
		}
	
		jsonObj.remove("bookCaseId");
		jsonObj.put("pushId", pushId+"");
		logger.info("debug {} : 书柜[{}] ,下发内容[{}]", jsonObj.getString("action"), bookCaseId, jsonObj.toJSONString());
		BookCaseChannelGroups.broadcast(bookCaseId, jsonObj.toJSONString());
		return null;
	}

}
